﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace MwanaDOC
{
    static class Program
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            LogToFile log = new LogToFile();
            DialogResult res = DialogResult.Abort;
            try
            {
                res = MessageBox.Show("Ocorreu um erro ao executar o sistema", "Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
                log.log("Program.cs-Principal", "ThreadException", e.Exception.Message, e.Exception.StackTrace);
            }
            finally
            {
                if (res == DialogResult.Abort)
                {
                    Environment.Exit(0);
                }
            }
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            LogToFile arqLog = new LogToFile();
            
            try
            {
                Exception ex = (Exception)e.ExceptionObject;
                MessageBox.Show("Ocorreu um erro ao executar o sistema","Application Error",MessageBoxButtons.OK,MessageBoxIcon.Stop);
                arqLog.log("Program.cs-Principal", "UnhandledException", ex.Message, ex.StackTrace);
                
            }
            finally
            {
                Environment.Exit(0);
            }
        }
    }
}
